package practice1_100;

import java.util.Arrays;

public class Practice213 {
    int[] count;

    public int rob(int[] nums) {
        if(nums.length==0) return 0;
        if(nums.length==1) return nums[0];
        count=new int[nums.length];
        Arrays.fill(count,-1);
        //针对无闭环的情况下，a即为返回的结果，
        //int a=select(nums,0);
        //针对闭环的情况，首位都不抢劫，首抢尾不抢，尾抢首不抢，
        int res1=select(Arrays.copyOfRange(nums,1,nums.length),0);
        Arrays.fill(count,-1);//因为count数组是公用的，求玩前面的，必须要清空
        int res2=select(Arrays.copyOfRange(nums,0,nums.length-1),0) ;
        return Math.max(res1,res2);

    }
    public int select(int[] nums,int index){
//        count=new int[nums.length];
//        Arrays.fill(count,-1);
        if(nums.length <=index) return 0;
        if (count[index]!=-1) return count[index];

        int sum=Math.max(nums[index]+select(nums,index+2),select(nums,index+1));
        count[index]=sum;
        return sum;
    }

    public static void main(String[] args) {
        int[] a=new int[] {200,3,140,20,10};
        int b=new Practice213().rob(a);
        System.out.println(b);
    }
}
